import 'dart:convert';

import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/iconfont/icon_font.dart' as icon;
import 'package:enter_prise_web/models/work_record.dart';
import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/work/work_controller.dart';
import 'package:enter_prise_web/widgets/NotFound.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart' as gets;
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

enum FilterType {
  all, //全部
  illegal, //不合法记录
  day, //查询过去一天
  week, //查询过去一周
  month, //查询过去一个月
  year, //查询过去一年
}

class WorkPage extends StatelessWidget {
  const WorkPage({super.key});

  @override
  Widget build(BuildContext context) {
    IndexController indexController = gets.Get.find();
    WorkController workController = gets.Get.find();
    return Container(
        padding: const EdgeInsets.all(8.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Stack(children: [
          Positioned(
            top: 16,
            right: 6,
            child: Text(
                "共有记录数:${workController.number.value} 总价值:${workController.price.value}元"),
          ),
          Positioned(
            top: 6,
            left: 6,
            child: Container(
              padding:
                  const EdgeInsets.only(left: 8, right: 8, top: 6, bottom: 6),
              decoration: BoxDecoration(
                color: const Color.fromRGBO(186, 214, 66, 1),
                borderRadius: BorderRadius.circular(5),
              ),
              child: GestureDetector(
                onTap: () {
                  indexController.showFilterView();
                },
                child: Row(
                  children: [
                    gets.Obx(() => Text(
                          indexController.filterText.value,
                          style: const TextStyle(fontSize: 16),
                        )),

                    const SizedBox(
                      width: 2,
                    ),
                    //漏斗Icon
                    icon.IconFont(
                      icon.IconNames.rrl_s_049,
                      color: "000000",
                      size: 22,
                    )
                  ],
                ),
              ),
            ),
          ),
          gets.GetBuilder<WorkController>(
              id: "work_list",
              builder: (controller) {
                // controller.number.value = workController.allMsg['itemsNum'];
                // controller.price.value = workController.allMsg['total'];
                return controller.workTodayRecords.isEmpty
                    ? const Center(
                        child: NotFound(
                          child: Text("您好像来到了一片不毛之地"),
                        ),
                      )
                    : Container(
                        padding: const EdgeInsets.only(top: 60),
                        child: ListView.builder(
                            itemCount: controller.workTodayRecords.length,
                            shrinkWrap: true,
                            itemBuilder: (context, index) {
                              var msg = controller.workTodayRecords[index];
                              RecordModel recordModel =
                                  RecordModel.fromJson(msg);
                              return Card(
                                color: controller.workTodayRecords[index]
                                            ['valid'] ==
                                        0
                                    ? Colors.red.withOpacity(0.3)
                                    : kDefaualtBgColor,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    ListTile(
                                      title:
                                          Text(recordModel.objectName ?? "无名称"),
                                      subtitle: Text(
                                          "${recordModel.owner}提交于 ${recordModel.time} "),
                                      trailing:
                                          Text("价值:${recordModel.value}元"),
                                    ),
                                    Divider(
                                      color: Colors.black45.withOpacity(0.3),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Text(
                                          "备注:${recordModel.comment ?? "无"}"),
                                    ),
                                    Divider(
                                      color: Colors.black45.withOpacity(0.3),
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        controller.workTodayRecords[index]
                                                    ['valid'] ==
                                                0
                                            ? TextButton(
                                                onPressed: () => {},
                                                child: const Text("审核通过"),
                                              )
                                            : Container(),
                                        TextButton(
                                          onPressed: () async {
                                            //弹出提示框
                                            showCupertinoDialog(
                                                context: Get.context!,
                                                builder: (c) {
                                                  return CupertinoAlertDialog(
                                                      title: const Text("删除"),
                                                      content:
                                                          const Text("确定删除吗？"),
                                                      actions: [
                                                        CupertinoDialogAction(
                                                            child: const Text(
                                                                "取消"),
                                                            onPressed: () {
                                                              Get.back();
                                                            }),
                                                        CupertinoDialogAction(
                                                            child: const Text(
                                                                "确定"),
                                                            onPressed:
                                                                () async {
                                                              var headers = {
                                                                'User-Agent':
                                                                    'Apifox/1.0.0 (https://apifox.com)',
                                                                'Content-Type':
                                                                    'application/json',
                                                                'Accept': '*/*',
                                                                'Host':
                                                                    'flycan.ren:8026',
                                                                'Connection':
                                                                    'keep-alive',
                                                                'Cookie':
                                                                    'JSESSIONID=2C05540819190BB6E7B35B9E4E54EFFD; UserIdentify=189h6ZzUt4cyjCK4OosphsFy8Ecml1Z/O5FvVI4pe4IsL6OVMxhuT71JVP9lExYjtEC9su1oGS9Ja1nH9mfDvWhhZN4IgdPa0Okvz2ORwCDSiI4ulvftSh/AsyzMNbswx9jEL+N+FmxJUJXAO+YRb9CnA4EezKtanEkKo9mO50WDz68Z0+VWxXCRkn/Tq+d+ZgaKpqmv5kb7D5rWNnjIzyfdXeKQkk0REUEcZKH2nJKETIJeM+lD92CmlWGbo1JhTZv0CCp+/qr58C7sI7RIPmruEX40GfpHY572e11Zl055Wxt+UY6dgUK3oIgOGVr714YS9ED2I4WcUbx1aSYCdOHLfnKZPczknEi8fUlSdDfZczTntOGAgtB6UJAFV9+c9ba1EcyWr31mDfLSE1fMq2/aOzrLTB8erf6YOMcKyILPfPbI73ssJ5eWoDcNKhHXN4k99sf8OFww2zW3VCfNcAa2Tc6UZqn58wJNirQHhpVEWRmtlmRZ7FUlQiXo4hUuL5be5Ya33b+Hv9lvBDNOcVW/YQE3kigeGjFiww=='
                                                              };
                                                              var request =
                                                                  http.Request(
                                                                      'POST',
                                                                      Uri.parse(
                                                                          'http://flycan.ren:8026/workRecord/deleteAppend'));
                                                              print("msg$msg");
                                                              request.body =
                                                                  json.encode(
                                                                      msg);
                                                              request.headers
                                                                  .addAll(
                                                                      headers);

                                                              http.StreamedResponse
                                                                  response =
                                                                  await request
                                                                      .send();

                                                              if (response
                                                                      .statusCode ==
                                                                  200) {
                                                                Fluttertoast
                                                                    .showToast(
                                                                        msg:
                                                                            '删除成功');
                                                                workController
                                                                    .updateData(
                                                                        indexController
                                                                            .filterType);
                                                              } else {
                                                                print(response
                                                                    .reasonPhrase);
                                                              }
                                                              Get.back();
                                                            })
                                                      ]);
                                                });

                                            // var response = await myDio.postData(
                                            //     "/workRecord/deleteAppend",
                                            //     data: formData);
                                            // print(response.statusCode);
                                            // if (response.data['statusCode'] ==
                                            //     200) {
                                            //   if (response.data['data'] != 0) {
                                            //     Fluttertoast.showToast(
                                            //         msg: "删除成功");
                                            //   } else {
                                            //     Fluttertoast.showToast(
                                            //         msg: "删除失败");
                                            //   }
                                            // } else {
                                            //   Fluttertoast.showToast(
                                            //       msg: "出现异常");
                                            // }
                                          },
                                          child: const Text("删除"),
                                        ),
                                      ],
                                    )
                                  ],
                                ),
                              );
                            }),
                      );
              }),
        ]));
  }
}
